#include <stdio.h>
#include <iostream>

namespace Y190613_Polynomial{
int main();

typedef struct PolyNode* Polynomial;
struct PolyNode {
	int coef;
	int expon;
	Polynomial link;
};
Polynomial Add(Polynomial P1, Polynomial P2);
Polynomial MultiByNode(Polynomial L1, Polynomial L2);

void Attach(int c, int e, Polynomial* pRear)
{
	if (c == 0)
		return;
	Polynomial P;

	P = (Polynomial)malloc(sizeof(struct PolyNode));
	P->coef = c;
	P->expon = e;
	P->link = NULL;
	(*pRear)->link = P;
	*pRear = P;
}

int Compare(int a, int b)
{
	if (a > b)
		return 1;
	if (a < b)
		return -1;
	return 0;
}

Polynomial ReadPoly() {
	Polynomial P, Rear, t;
	int N;
	int c, e;
	scanf("%d", &N);
	P = (Polynomial)malloc(sizeof(struct PolyNode));
	P->coef = 0;
	P->expon = 0;
	P->link = NULL;
	Rear = P;
	while (N--) {
		scanf("%d %d", &c, &e);
		Attach(c, e, &Rear);
	}
	t = P; P = P->link; free(t);
	return P;
}

Polynomial Mult(Polynomial L1, Polynomial L2) {
	Polynomial L, t1, t2, temp;
	L = (Polynomial)malloc(sizeof(struct PolyNode));
	L->coef = 0;
	L->expon = 0;
	L->link = NULL;
	t1 = L1;
	t2 = L2;
	if (t1 != NULL && t2 != NULL) {
		temp = MultiByNode(L1, t2);
		t2 = t2->link;
		while (t2) {
			Polynomial temp2 = MultiByNode(L1, t2);
			temp = Add(temp, temp2);
			t2 = t2->link;
		}
		L = temp;
	}
	return L;
}
Polynomial MultiByNode(Polynomial L1, Polynomial L2) {
	Polynomial L, head, t1;
	L = (Polynomial)malloc(sizeof(struct PolyNode));
	L->coef = 0;
	L->expon = 0;
	L->link = NULL;
	head = L;
	t1 = L1;
	while (t1) {
		Attach(t1->coef * L2->coef, t1->expon + L2->expon, &head);
		t1 = t1->link;
	}
	head = L->link;
	free(L);
	return head;
}

Polynomial Add(Polynomial P1, Polynomial P2) {
	Polynomial t1 = P1, t2 = P2;
	if (t1 == NULL || t2 == NULL)
		return NULL;
	Polynomial front = (Polynomial)malloc(sizeof(struct PolyNode));
	front->link = NULL;
	front->coef = 0;
	front->expon = 0;
	Polynomial Rear = front;

	while (t1 && t2) {
		switch (Compare(t1->expon, t2->expon)) {
		case 1:
			Attach(t1->coef, t1->expon, &Rear);
			t1 = t1->link;
			break;
		case -1:
			Attach(t2->coef, t2->expon, &Rear);
			t2 = t2->link;
			break;
		case 0:
			Attach(t1->coef + t2->coef, t1->expon, &Rear);
			t1 = t1->link;
			t2 = t2->link;
			break;
		}
	}
	for (; t1; t1 = t1->link)Attach(t1->coef, t1->expon, &Rear);
	for (; t2; t2 = t2->link)Attach(t2->coef, t2->expon, &Rear);
	t1 = front->link;
	free(front);
	return t1;
}

void PrintPoly(Polynomial P) {
	Polynomial t = P;
	if (t == NULL)
	{
		printf("0 0\n");
		return;
	}
	int flag = 0;
	while (t) {
		if (flag == 0)
			flag = 1;
		else
			printf(" ");
		printf("%d %d", t->coef, t->expon);
		t = t->link;
	}
	printf("\n");
}


int main() {
	Polynomial P1, P2, PP, PS;
	P1 = ReadPoly();
	P2 = ReadPoly();
	//PrintPoly(P1);
	//PrintPoly(P2);
	PP = Mult(P1, P2);
	PrintPoly(PP);
	PS = Add(P1, P2);
	PrintPoly(PS);
	return 0;
}

};

int main_Y190613_Polynomial()
{
	Y190613_Polynomial::main();
	return 0;
}